jQuery.noConflict();

function initMenu() {
    jQuery('#menu ul ul').hide();
    jQuery("#menu .active").parents("li").children("ul").show();
    jQuery('#menu ul li').click(function(event) {
        jQuery(this).parent().find("ul").slideUp('fast');
        jQuery(this).parent().find("li").children("a").removeClass("active");
        jQuery(this).find("ul").slideToggle('fast');
        //Avoid submenus from having class 'current' before navigation
        if(jQuery(this).find("ul").length > 0){
            jQuery(this).children("a").toggleClass("active");
        }
        //Avoid submenus from moving before navigation
        event.stopPropagation();
    });
}

function beforeSend(){
    jQuery(".ajaxified").html("Un momento por favor...");
}

function closeSession(){
    jQuery.post("index.php?option=com_user&view=login&Itemid=2", "option=com_user&task=logout&return=qwerty");
    return true;
}

function RealTypeOf(v) {
    if (typeof(v) == "object") {
        if (v === null) return "null";
        if (v.constructor == (new Array).constructor) return "array";
        if (v.constructor == (new Date).constructor) return "date";
        if (v.constructor == (new RegExp).constructor) return "regex";
        return "object";
    }
    return typeof(v);
}

function FormatJSON(oData, sIndent) {
    if (arguments.length < 2) {
        sIndent = "";
    }
    var sIndentStyle = "    ";
    var sDataType = RealTypeOf(oData);
    var sHTML = "";
    var iCount = 0;

    // open object
    if (sDataType == "array") {
        if (oData.length == 0) {
            return "[]";
        }
        sHTML = "[";
    } else {
        iCount = 0;
        jQuery.each(oData, function() {
            iCount++;
            return;
        });
        if (iCount == 0) { // object is empty
            return "{}";
        }
        sHTML = "{";
    }

    // loop through items
    iCount = 0;
    jQuery.each(oData, function(sKey, vValue) {
        if (iCount > 0) {
            sHTML += ",";
        }
        if (sDataType == "array") {
            sHTML += ("\n" + sIndent + sIndentStyle);
        } else {
            sHTML += ("\n" + sIndent + sIndentStyle + "\"" + sKey + "\"" + ": ");
        }

        // display relevant data type
        switch (RealTypeOf(vValue)) {
            case "array":
            case "object":
                sHTML += FormatJSON(vValue, (sIndent + sIndentStyle));
                break;
            case "boolean":
            case "number":
                sHTML += vValue.toString();
                break;
            case "null":
                sHTML += "null";
                break;
            case "string":
                sHTML += ("\"" + vValue + "\"");
                break;
            default:
                sHTML += ("TYPEOF: " + typeof(vValue));
        }

        // loop
        iCount++;
    });

    // close object
    if (sDataType == "array") {
        sHTML += ("\n" + sIndent + "]");
    } else {
        sHTML += ("\n" + sIndent + "}");
    }

    // return
    return sHTML;
}
 
jQuery(document).ready(function($) {

    $(".cerrar-sesion").click(function(){
        closeSession();
        window.location.href = this.href;
    });
            
    initMenu(); // Initialize the menu!
    $(".notification").click(function (){
        $(this).hide();
    });

   $('#dialog').dialog({
        autoOpen: false,
        width: 650,
        buttons: {
            "Enviar": function() {
                $(this).dialog("close");
            },
            "Cancelar": function() {
                $(this).dialog("close");
            }
        }
    }); // Default dialog. Each should have it's own instance.
			
    $('.dialog_link').click(function(){
        $('#dialog').dialog('open');
        return false;
    }); // Toggle dialog
	
    $('.notification').hover(function() {
        $(this).css('cursor','pointer');
    }, function() {
        $(this).css('cursor','auto');
    }); // Close notifications
			
    $('.checkall').change(function(){
        
        $(this).parent().parent().parent().parent().find("input[type='checkbox']").attr('checked', jQuery(this).is(':checked'));
    }); // Top checkbox in a table will select all other checkboxes in a specified column
			
    $('.notification span').click(function() {
        $(this).parents('.notification').fadeOut(800);
    }); // Close notifications on clicking the X button
			
			
    $('.fade_hover').hover(
        function() {
            $(this).stop().animate({
                opacity:0.6
            },200);
        },
        function() {
            $(this).stop().animate({
                opacity:1
            },200);
        }
        ); // The fade function
			
    //sortable, portlets
    $(".column").sortable({
        connectWith: '.column',
        placeholder: 'ui-sortable-placeholder',
        forcePlaceholderSize: true,
        scroll: false,
        helper: 'clone'
    });
				
    $(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all").find(".portlet-header").addClass("ui-widget-header ui-corner-all").prepend('<span class="ui-icon ui-icon-circle-arrow-s"></span>').end().find(".portlet-content");

    $(".portlet-header .ui-icon").click(function() {
        $(this).toggleClass("ui-icon-minusthick");
        $(this).parents(".portlet:first").find(".portlet-content").toggle();
    });

    $(".column").disableSelection();
	
    $("table.stats").each(function() {

        var statsType = 'area';

        if($(this).attr('rel')) {
            statsType = $(this).attr('rel');
        }
		
        //        var chart_width = ($(this).parent().parent(".ui-widget").width()) - 60;
        $(this).hide().visualize({
            type: statsType,	// 'bar', 'area', 'pie', 'line'
            width: '800px',
            height: '240px',
            colors: ['#6fb9e8', '#ec8526', '#9dc453', '#ddd74c']
        }); // used with the visualize plugin. Statistics.
    });
			
    $(".tabs").tabs(); // Enable tabs on all '.tabs' classes
	
    $( ".datepicker" ).datepicker({
        dateFormat:"dd/mm/yy",
        changeMonth: true,
        changeYear: true
    });

    // Slider
    $(".slider").slider({
        range: true,
        values: [20, 70]
    });
				
    // Progressbar
    $(".progressbar").progressbar({
        value: 40
    });

    $("#sombreadoAcademico").tabs({
        selected:2
    });
   
});

